<?php
namespace App\Http\Middleware;

use Closure;
use Log;
use Request;

class BaseMiddleware {    

	/**
	 * Create a new filter instance.
	 *
	 * @param  Guard  $auth
	 * @return void
	 */
	public function __construct()
	{
	    //
	}

	/**
	 * Handle an incoming request.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Closure  $next
	 * @return mixed
	 */
	public function handle($request, Closure $next, $guard = null)
	{
        Log::info('REQUEST BEGIN');
        $env=array();
        foreach($_SERVER as $key=>$value){
            if(in_array($key,[
                'REQUEST_URI','HTTP_HOST','SERVER_ADDR','REQUEST_METHOD','HTTP_COOKIE'   
            ]) || strpos($key,'_X_')!==false ) {
                $env[$key] = $value;
            }
        }
        Log::info('request env', $env);
        if(!empty($_POST)) Log::info('request _POST:', $_POST);
        if(!empty($_FILES)) Log::info('request _FILES:', $_FILES);
        $content = file_get_contents("php://input");
        if(!empty($content)) Log::info('request body:', [$content]);
		$begin_time = microtime(true);
        $response =  $next($request);
        $end_time = microtime(true);
        $memory =  round(memory_get_usage()/1024/1024,3);
        Log::info('REQUEST END : ', ['time' => round($end_time - $begin_time,4).'s','memory'=>$memory.'M']);
        $response->header('Environment',json_encode(getenv('APP_ENV')));

		return $response->header('Serverd', $_SERVER['SERVER_ADDR']);
	}

}
